import { dialog, ipcMain, IpcMainEvent } from 'electron';
import { Environment } from '../environment/environment';
import { ctl } from '../global';
import { store } from '../store';
import { Http } from '../utils';

/**
 * 通信服务基础
 *
 * @author chitanda
 * @date 2022-06-08 16:06:26
 * @export
 * @class IPC
 */
export class IPCHelper {

  /**
   * Creates an instance of IPC.
   *
   * @author chitanda
   * @date 2022-06-09 15:06:27
   */
  constructor() {
    this.on();
  }

  /**
   * 最小化
   *
   * @author chitanda
   * @date 2022-06-09 15:06:09
   * @protected
   */
  protected min(): void {
    ctl.min();
  }

  /**
   * 最大化
   *
   * @author chitanda
   * @date 2022-06-07 14:06:28
   * @protected
   */
  protected max(): void {
    ctl.max();
  }

  /**
   * 关闭窗口
   *
   * @author chitanda
   * @date 2022-06-07 14:06:32
   * @protected
   */
  protected close(): void {
    ctl.close();
  }

  /**
   * 打开 MosDynamic 窗口
   *
   * @author chitanda
   * @date 2022-06-09 15:06:48
   * @protected
   * @param {IpcMainEvent} _evt
   * @param {string} psDevSlnSys
   * @param {boolean} [manage]
   */
  protected openMosDynamic(_evt: IpcMainEvent, psDevSlnSys: string, manage?: boolean): void {
    ctl.openMosDynamicWindow(psDevSlnSys, manage);
  }

  /**
   * 登录
   *
   * @author chitanda
   * @date 2022-06-09 17:06:30
   * @protected
   * @param {IpcMainEvent} _evt
   * @param {string} loginname
   * @param {string} password
   * @return {*}  {Promise<void>}
   */
  protected async login(_evt: IpcMainEvent, loginname: string, password: string): Promise<void> {
    try {
      const data = await Http.post(`${Environment.AppCenterUrl}/v7/login`, { loginname, password });
      if (data) {
        const token = data.token;
        if (token) {
          store.set('token', data.token);
          ctl.closeLoginWindow();
        } else {
          dialog.showMessageBox({
            type: 'warning',
            title: '登录失败',
            message: '未能正确从后台返回认证信息',
          });
        }
      }
    } catch (err: any) {
      dialog.showMessageBox({
        type: 'warning',
        title: '登录失败',
        message: err.message,
      });
    }
  }

  /**
   * 登出当前用户
   *
   * @author chitanda
   * @date 2022-06-10 11:06:22
   * @protected
   */
  protected logout(): void {
    store.delete('token');
    ctl.openLoginWindow();
  }

  /**
   * 设置 token 值
   *
   * @author chitanda
   * @date 2022-06-09 17:06:36
   * @protected
   * @param {IpcMainEvent} evt
   * @param {string} token
   */
  protected setToken(evt: IpcMainEvent, token: string): void {
    store.set('token', token);
    ctl.closeLoginWindow();
    evt.returnValue = true;
  }

  /**
   * 监听事件
   *
   * @author chitanda
   * @date 2022-06-07 14:06:38
   * @protected
   */
  protected on(): void {
    this.min = this.min.bind(this);
    this.max = this.max.bind(this);
    this.close = this.close.bind(this);
    this.openMosDynamic = this.openMosDynamic.bind(this);
    this.login = this.login.bind(this);
    this.logout = this.logout.bind(this);
    // 绑定事件
    ipcMain.on('min', this.min);
    ipcMain.on('max', this.max);
    ipcMain.on('close', this.close);
    ipcMain.on('openMosDynamic', this.openMosDynamic);
    ipcMain.on('setToken', this.setToken);
    ipcMain.on('login', this.login);
    ipcMain.on('logout', this.logout);
  }
}
